package com.fanben100.biz;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fanben100.util.PasswordEncoder;
import com.fanben100.util.StringUtil;

/**
 * 请求参数生成器
 * 
 * @author Administrator
 * 
 */
public class RequestBuilder {
	private String url = "http://www.fanben100.net/shop.php";

	private static final String keyStore = "fanben100_%#@12312googleBaiduJJJJJJRi";

	//private Map<String, String> keyMap = new ConcurrentHashMap<String, String>();

	public RequestBuilder(String... url) {
		if (url != null && url.length > 0) {
			this.url = "";
			for (int i = 0; i < url.length; i++) {
				if (!StringUtil.isNullOrEmpty(url[i])) {
					this.url += url[i];
				}
			}
		}
		// addTimestamp();
	}

	public RequestBuilder directLogin() {
		this.url += "?class=super&method=login";
		//this.keyMap.put("class", "super");
		//this.keyMap.put("method", "login");
		return this;
	}

	public RequestBuilder addTimestamp() {
		long timestamp = System.currentTimeMillis();
		Date now = new Date();
		DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,
				DateFormat.FULL); // 显示日期，周，时间（精确到秒）
		String str5 = d5.format(now);
		Date nowSecondDate = null;
		try {
			nowSecondDate = d5.parse(str5);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String secondStr = String.valueOf(nowSecondDate.getTime());
		secondStr = secondStr.substring(0, secondStr.length() - 3);
		this.url += "&time=" + secondStr;// nowSecondDate.getTime();
		//this.keyMap.put("time", String.valueOf(timestamp));
		return this;
	}

	public RequestBuilder addLoginId(Serializable loginId) {
		this.url += "&id=" + loginId;
		//this.keyMap.put("id", String.valueOf(loginId));
		return this;
	}

	public RequestBuilder addLoginPassword(String password) {
		this.url += "&password=" + password;
		//this.keyMap.put("password", password);
		return this;
	}

	public RequestBuilder build(String paramName, String val) {
		if (this.url.indexOf("?") == -1) {
			this.url += "?" + paramName + "=" + val;
		} else {
			this.url += "&" + paramName + "=" + val;
		}
		//this.keyMap.put(paramName, val);
		return this;
	}

	@Override
	public String toString() {
		this.buildKey();
		//System.out.println("request url is:" + this.url);
		return this.url;
	}

	public void buildKey() {
		addTimestamp();
		StringBuffer sbMw = new StringBuffer();
		// for (Map.Entry<String, String> entry : keyMap.entrySet()) {
		// String param = entry.getKey();
		// if (!"time".equals(param)) {
		// String val = entry.getValue();
		// //sbMw.append("|").append(param).append("|").append(val);
		// }
		// }
		Pattern p = Pattern.compile("(\\?|&+)(.+?)=([^&]*)");
		Matcher m = p.matcher(this.url);
		while (m.find()) {
			sbMw.append("|").append(m.group(2)).append("|").append(m.group(3));
			// System.out.println(m.group(2) + "=" + m.group(3));
		}
		// sbMw.append("|time|").append(this.keyMap.get("time"));
		String mwDispStr = sbMw.substring(1);
		String mwStr = PasswordEncoder.getMD5(mwDispStr.getBytes());
		String mwFinalStr = PasswordEncoder.getMD5((mwStr + keyStore)
				.getBytes());
		this.url += "&" + "key" + "=" + mwFinalStr;
	}

	public static void main(String[] args) {
		String s = "http://..../.../example.jsp?data={paramA=a,paramB=b,paramC=c}";
		String s1 = "(.*?)\\{(.*?)\\}";
		Pattern p = Pattern.compile(s1);
		Matcher m = p.matcher(s);
		String sub = "";
		if (m.find()) {
			sub = m.group(2);
		}
		String[] array = sub.split(",");
		Map<String, Object> map = new HashMap<String, Object>();

		for (String ss : array) {
			map.put(ss.substring(0, ss.indexOf("=")),
					ss.substring(ss.indexOf("=") + 1));
		}
		String aid = "http://127.0.0.1:8080/??sdf=s&&st=b=&&?sw?=%B9%FA+%BC%D2&tb=&st=9";
		p = Pattern.compile("(\\?|&+)(.+?)=([^&]*)");
		m = p.matcher(aid);
		while (m.find()) {
			System.out.println(m.group(2) + "=" + m.group(3));
		}
	}
}
